<?php

namespace App\Http\Controllers\Admin\Casts;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Casts\Episode;
use App\Models\Casts\Category;


class EpisodeController extends Controller
{

    /**
     * 课程列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $categories = Category::get_categories();

        //如果没有选择栏目，则显示所有栏目
        if (!$request->has('category_id') || $request->category_id == '') {
            //查找
            $where = function ($query) use ($request) {
                if ($request->has('keyword') and $request->keyword != '') {
                    $search = "%" . $request->keyword . "%";
                    $query->where('name', 'like', $search);
                }
            };

            $episodes = Episode::with('category')->where($where)->orderBy('created_at', 'desc')
                ->paginate(config('admin.page_size'));
            return view('admin.casts.episode.index', compact('categories', 'episodes'));
        }

        //如果选择栏目了，则显示对应的课程章节
        $category_id = $request->category_id;
        $episodes = Episode::get_episodes($request);
        return view('admin.casts.episode.show', compact('categories', 'episodes', 'category_id'));
    }

    /**
     * 保存
     * @param Request $request
     * @return array
     */
    public function store(Request $request)
    {
        Episode::create($request->all());
        return ['status' => 1, 'episodes' => Episode::get_episodes($request)];
    }

    /**
     * 编辑
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $episode = Episode::find($id);
        return $episode;
    }

    /**
     * 更新
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, $id)
    {
        $episode = Episode::find($id);
        $episode->update($request->all());
        return ['status' => 1, 'episodes' => Episode::get_episodes($request)];
    }

    /**
     * 删除
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    function destroy(Request $request, $id)
    {
        if (!Episode::check_children($id)) {
            return ['status' => 0, 'msg' => '当前分类有子分类，请先将子分类删除后再尝试删除~'];
        }

        Episode::destroy($id);
        return ['status' => 1, 'episodes' => Episode::get_episodes($request)];
    }

    /**
     * 排序
     * @param Request $request
     * @return array
     */
    function sort_order(Request $request)
    {
        $sort_order = json_decode($request->sort_order);

        //一级
        foreach ($sort_order as $key => $value) {
            Episode::sort_order($value->id, 0, $key);

            //二级
            if (isset($value->children)) {
                foreach ($value->children as $k => $v) {
                    Episode::sort_order($v->id, $value->id, $k);
                }
            }
        }
    }
}
